package com.evon.yardmanagement.service;

import java.util.List;

import com.evon.yardmanagement.model.ParkingSlot;
import com.evon.yardmanagement.model.Section;
import com.evon.yardmanagement.dao.ParkingSlotDAO;
import com.evon.yardmanagement.dao.SectionDAO;
import com.evon.yardmanagement.model.ParkingSlot;
import com.evon.yardmanagement.transformer.BaseTransformer;
import com.evon.yardmanagement.ui.dto.ParkingSlotDTO;

public class ParkingSlotsService {

	ParkingSlotDAO psDAO = new ParkingSlotDAO();

	BaseTransformer<ParkingSlot, ParkingSlotDTO> transformer = new BaseTransformer<ParkingSlot, ParkingSlotDTO>(
			ParkingSlot.class, ParkingSlotDTO.class);

	public boolean addParkingSlots(ParkingSlot ps) {
		// TODO Auto-generated method stub
		try {
			psDAO.saveOrUpdate(ps);
			return true;
		} catch (Exception e) {
			return false;
		}

	}

	public boolean addParkingSlots(List<ParkingSlotDTO> psDTO, int sectionId) {
		if (psDTO == null || psDTO.isEmpty()) {
			return false;
		}
		List<ParkingSlot> psList = transformer.transformToModel(psDTO);
		Section section = new SectionDAO().findById(sectionId);
		boolean isSuccess = true;
		for (ParkingSlot ps : psList) {
			ps.setSection(section);
			isSuccess = isSuccess && addParkingSlots(ps);
		}

		return isSuccess;
	}

	public List<ParkingSlot> getAllParkingslSlots() {
		// TODO Auto-generated method stub
		return psDAO.findAll();
	}

	public List<ParkingSlot> fetchParkingSlotBySectionId(Integer id) {
		Section section = new Section();
		section.setId(id);
		return psDAO.findByProperty("section", section);
	}
	
	public boolean setActiveParkingSlotByYardId(int yardId) {
		// TODO Auto-generated method stub
	
		
			psDAO.setActive(yardId);
		
		return true;
	}
	
	public boolean setDeActiveParkingSlotByYardId(int yardId) {
		// TODO Auto-generated method stub
	
		
		psDAO.setDeActive(yardId);
		
		return true;
	}
	
	/**
	 * Save the list of sections in DB.
	 * @param data
	 */
	public void importParkingSlots(List<ParkingSlot> data) {
		 if(data != null && !data.isEmpty()){
			 for(ParkingSlot ps : data){
				 psDAO.saveOrUpdate(ps);
			 }
		 }
		
	}	
}
